package com.imcrl.component.drager
{
	import com.imcrl.core.IDatable;
	import com.imcrl.event.DragAndDropEvent;
	
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import starling.display.Sprite;
	import starling.events.Event;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	
	/**
	 * 可拖放动物品基类;
	 * @author crl
	 * 
	 */	
	public class AbstractDragger extends Sprite implements IDatable
	{
		private static const MIN_DISTANCE_SQ:int=9;
		
		private var _dropTrigger:Boolean;
		private var _dragEnabled:Boolean;
		
		protected var _data:*;
		public function AbstractDragger()
		{
			super();
		}
		
		/**
		 * 设置是否接受拖曳; 
		 * @param b
		 * 
		 */		
		public function setDragEnabled(b:Boolean):void{
			if(b){
				addEventListener(TouchEvent.TOUCH, touchHandle);
			}else{
				removeEventListener(TouchEvent.TOUCH, touchHandle);
			}
			_dragEnabled = b;
		}
		public function get dragEnabled():Boolean{
			return _dragEnabled;
		}
		
		/**
		 * 是否为拖拽目标;
		 * @param b
		 * 
		 */		
		public function setDropTrigger(b:Boolean):void{
			_dropTrigger = b;
		}
		public function get dropTrigger():Boolean{
			return _dropTrigger;
		}
		
		
		public function isDragAcceptableInitiator(drager:AbstractDragger,data:*):Boolean{
			return true;
		}
		/**
		 * 取 
		 * @return 
		 * 
		 
		 public function getMousePosition():Point{
		 return new Point(mouseX, mouseY);
		 } */
		
		/**
		 * 取得全局坐标; 
		 * @param rv
		 * @return 
		 * 
		 */	    
		public function getGlobalLocation(rv:Point=null):Point{
			if(!rv){
				rv=new Point(0, 0);
			}
			return localToGlobal(rv);
		}
		
		private var mouseIsDown:Boolean=false;
		private function touchHandle(e:TouchEvent):void{
			var touch:Touch = e.getTouch(this);
			if (touch == null) return;
			var m:Point;
			if (touch.phase == TouchPhase.BEGAN && !mouseIsDown)
			{
				m=touch.getLocation(this);
				pressingMX = m.x;
				pressingMY = m.y;
				mouseIsDown=true;
			}
			else if (touch.phase == TouchPhase.MOVED && mouseIsDown)
			{
				var buttonRect:Rectangle = getBounds(stage);
				if (touch.globalX < buttonRect.x ||
					touch.globalY < buttonRect.y ||
					touch.globalX > buttonRect.x + buttonRect.width ||
					touch.globalY > buttonRect.y + buttonRect.height)
				{
					mouseIsDown = false;
					return;
				}
				m=touch.getLocation(this);
				__mouseMove(m.x,m.y,e);
			}
			else if (touch.phase == TouchPhase.ENDED && mouseIsDown)
			{
				mouseIsDown=false;
				dispatchEvent(new Event(Event.TRIGGERED, true));
			}
		}
		
		private var pressingMX:Number;
		private var pressingMY:Number;
		private function __mouseMove(mouseX:Number,mouseY:Number,e:TouchEvent):void{
			var dx:Number=mouseX-pressingMX;
			var dy:Number=mouseY-pressingMY
			if( ( dx*dx + dy*dy ) > MIN_DISTANCE_SQ ){
				fireDragRecognizedEvent(e);
				mouseIsDown=false;
			}
		}
		
		/**
		 * 触发用户想拖动事件; 
		 * @param touchedChild
		 * 
		 */		
		private function fireDragRecognizedEvent(e:TouchEvent):void{
			dispatchEvent(new DragAndDropEvent(DragAndDropEvent.DRAG_RECOGNIZED, this,null,null,e));
		}
		
		public function get data():*
		{
			return _data;
		}
		
		public function set data(value:*):void
		{
			_data=value;
		}
		
	}
}